package com.warm.mini.wework.controller.invoke;

import com.warm.mini.wework.config.WeworkConfigProperties;
import com.warm.mini.wework.config.TanzsayConfig;
import com.warm.mini.wework.service.invoke.WeworkInvokeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description tanzsay invoke event
 * @Author warmchuang
 * @Date 2022/2/7 17:27
 */
@Slf4j
@RequestMapping(value = "/open/v1/invoke/tanzsay/")
@RestController
public class TanzsayInvokeController {

    @Resource
    private TanzsayConfig tanzsayConfig;
    @Resource
    private WeworkInvokeServiceImpl weworkInvokeService;

    /**
     * 通讯录回调事件
     * @param msgSignature 企业微信加密签名
     * @param timestamp 时间戳
     * @param nonce 随机数
     * @param echostr 加密的字符串
     * @param xmlBody post 请求的 body
     *   回调的 Content-Type: text/xml --> @RequestBody(required = false) String body, 用 String 不能使用对象来接收
     *   目前只支持 xml 回调, 不支持 json 回调
     * @return
     */
    @RequestMapping(value = "changeContact")
    public Object changeContact(@RequestParam(name = "msg_signature", required = false) String msgSignature,
                              @RequestParam(name = "timestamp", required = false) String timestamp,
                              @RequestParam(name = "nonce", required = false) String nonce,
                              @RequestParam(name = "echostr", required = false) String echostr,
                              @RequestBody(required = false) String xmlBody) {
        log.info("wework changeContact msg_signature:{} timestamp:{} nonce:{} echostr:{}, body:[{}]",
                msgSignature, timestamp, nonce, echostr, xmlBody);

        WeworkConfigProperties config = tanzsayConfig.contactConfig();
        return weworkInvokeService.invokeMsgHandle(msgSignature, timestamp, nonce, echostr, xmlBody, config);
    }

    @RequestMapping(value = "changeExternalContact")
    public Object changeExternalContact(@RequestParam(name = "msg_signature", required = false) String msgSignature,
                              @RequestParam(name = "timestamp", required = false) String timestamp,
                              @RequestParam(name = "nonce", required = false) String nonce,
                              @RequestParam(name = "echostr", required = false) String echostr,
                              @RequestBody(required = false) String xmlBody) {
        log.info("wework changeExternalContact msg_signature:{} timestamp:{} nonce:{} echostr:{}, body:[{}]",
                msgSignature, timestamp, nonce, echostr, xmlBody);
        WeworkConfigProperties config = tanzsayConfig.externalContactConfig();
        return weworkInvokeService.invokeMsgHandle(msgSignature, timestamp, nonce, echostr, xmlBody, config);
    }
}
